
package com.uit.ecommerce.weatherservice.servlets;

import com.uit.ecommerce.services.Exception_Exception;
import com.uit.ecommerce.services.ServiceLocation;
import com.uit.ecommerce.services.ServiceLocationService;
import com.uit.ecommerce.services.ServiceWeather;
import com.uit.ecommerce.services.ServiceWeatherService;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.WebServiceRef;

@WebServlet(name = "servWeatherArea", urlPatterns = {"/servWeatherArea"})
public class servWeatherArea extends HttpServlet {
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Weather_Webservice_Server/ServiceWeatherService.wsdl") 
                private ServiceWeatherService serviceweather;
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/Weather_Webservice_Server/ServiceLocationService.wsdl") 
       private ServiceLocationService serviceLocation;
    /**
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception_Exception {
         request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
         String action=request.getParameter("action");
        try {
            if(action.equalsIgnoreCase("view")){
               ViewAreaWeather(request, response, session);
             }
            else if(action.equalsIgnoreCase("future")){
                ViewAreaWeather3Day(request, response, session);
            }
            
        } finally {            
            out.close();
        }
    }
private void ViewAreaWeather(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception_Exception, IOException{
    ServiceWeather ws=serviceweather.getServiceWeatherPort();
               ArrayList<com.uit.ecommerce.services.WeatherAreaDTO> area=(ArrayList<com.uit.ecommerce.services.WeatherAreaDTO>) ws.getAllAreaWeather();
               session.setAttribute("area", area);
               String path = "./jsps/ThoiTietKhuVuc.jsp";
               response.sendRedirect(path);
}
private void ViewAreaWeather3Day(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception_Exception, IOException{
                ServiceWeather ws=serviceweather.getServiceWeatherPort();
                String area=request.getParameter("area");
                String areaid=request.getParameter("areacode");
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
                java.util.Date date = new java.util.Date();
                String today=dateFormat.format(date);
                String aftertoday=dateFormat.format(new Date(date.getYear(), date.getMonth(), date.getDate()+1));
                String aftertoday1=dateFormat.format(new Date(date.getYear(), date.getMonth(), date.getDate()+2));   
                com.uit.ecommerce.services.WeatherAreaDTO weathertoday=(com.uit.ecommerce.services.WeatherAreaDTO) ws.getWeatherAreaByDateAndName(today, area);
                session.setAttribute("today", weathertoday);
                com.uit.ecommerce.services.WeatherAreaDTO weatheraftertoday=(com.uit.ecommerce.services.WeatherAreaDTO) ws.getWeatherAreaByDateAndName(aftertoday, area);
                session.setAttribute("aftertoday", weatheraftertoday);
                com.uit.ecommerce.services.WeatherAreaDTO weatheraftertoday1=(com.uit.ecommerce.services.WeatherAreaDTO) ws.getWeatherAreaByDateAndName(aftertoday1, area);
                session.setAttribute("aftertoday1", weatheraftertoday1);
                com.uit.ecommerce.services.WeatherAreaDTO mintemp=(com.uit.ecommerce.services.WeatherAreaDTO) ws.getWeatherAreaMinTemp(areaid);
                session.setAttribute("mintemp", mintemp);
                com.uit.ecommerce.services.WeatherAreaDTO maxtemp=(com.uit.ecommerce.services.WeatherAreaDTO) ws.getWeatherAreaMaxTemp(areaid);
                session.setAttribute("maxtemp", maxtemp);
                String path = "./jsps/ThoiTietKhuVuc3Ngay.jsp";
               response.sendRedirect(path);
}
   /** 
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(servWeatherArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception_Exception ex) {
            Logger.getLogger(servWeatherArea.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * 
     * @return 
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
